﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using XiaoYuStock.Core.Entities;
namespace XiaoYuStock.Core
{

    /// <summary>
    /// https://hq.finance.ifeng.com/q.php?l=sh600035
    /// 返回值：
    /// var json_q={"sz000002":
    ///[
    ///29.30,last
    ///27.56,昨收close
    ///1.74,
    ///6.31,
    ///27.50,open
    ///30.22,high
    ///27.47,low
    ///29.29,
    ///29.30,
    ///155049568.00,volume /100(手)
    ///4549589504.00,amount(万元)
    ///29.29,
    ///29.28,
    ///29.27,
    ///29.26,
    ///29.25,
    ///17400.00,
    ///44300.00,
    ///16400.00,9800.00,14200.00,29.30,29.33,29.35,29.38,29.40,401303.00,300.00,500.00,1080.00,40400.00,0.00,0.00,0.00,1505459043,1505459044,29.30],"s_sh000001":[3353.62,3371.43,-17.810,-0.53],"s_sz399001":[11063.10,11070.49,-7.39,-0.07],"s_sz399300":[3831.30,3829.96,1.34,0.03],"s_hk10000":[27761.330,27777.199,-15.870,-0.060],"s_int_dji":[22268.34,22203.48,64.86,0.29],"s_sh601600":[8.09,8.09,0.000,0.00],"s_sh600516":[33.74,36.83,-3.090,-8.39],"s_sh600111":[17.62,18.81,-1.190,-6.33],"s_sz000413":[11.08,11.29,-0.21,-1.86],"s_sz000930":[14.34,14.36,-0.02,-0.14],"s_sh601318":[53.38,54.02,-0.640,-1.18],"ind400130025":[-0.534,1505459017,1505459032],"ind400130163":[0.278,1505459017,1505459032],"ind400129866":[0.006,1505459017,1505459032],"ind400129762":[-0.289,1505459017,1505459032],"ind400130015":[0.853,1505459017,1505459032],"ind400130006":[-0.335,1505459017,1505459032],"ind400124989":[-0.127,1505459017,1505459031],"ind400124673":[0.801,1505459017,1505459031],"ind400122012":[-0.283,1505459017,1505459031],"ind400123154":[-0.101,1505459017,1505459031],"s_sh600036":[25.97,25.86,0.110,0.43],"s_sh601857":[7.98,7.96,0.020,0.25],"s_sh600028":[5.92,5.95,-0.030,-0.50],"s_sh601398":[5.77,5.88,-0.110,-1.87],"s_sh600016":[8.23,8.24,-0.010,-0.12],"s_sh600030":[18.03,17.97,0.060,0.33],"s_sh600050":[7.31,7.48,-0.170,-2.27],"s_sh601899":[4.01,4.01,0.000,0.00],"s_sh600000":[12.83,12.85,-0.020,-0.16],"s_sz000001":[11.29,11.32,-0.03,-0.27],"s_sh600048":[11.65,11.32,0.330,2.92],"s_sh601766":[9.88,9.96,-0.080,-0.80],"s_sz002024":[11.77,11.84,-0.07,-0.59]};
    ///hq.show();
    /// 
    /// 
    /// 
    /// http://finance.ifeng.com/app/hq/stock/sz002109/
    /// </summary>
    public class HistoryDataServiceExtensions
    {

        private static Regex regTR = new Regex("<tr.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static Regex regTD = new Regex("<td.*?</td>", RegexOptions.IgnoreCase | RegexOptions.Singleline);


        private static Regex regKeyValue = new Regex("(-?\\d+\\.?\\d+)\\((-?\\d+\\.?\\d+%)\\)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        /// <summary>
        /// 返回股票数据
        /// </summary>
        /// <param name="sharesCode"></param>
        /// <returns></returns>
        public static TradeDataExtensions LoadTransferData(string sharesCode)
        {
            string url = string.Format("http://finance.ifeng.com/app/hq/stock/{0}/", sharesCode);
            string htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("UTF-8"));
            return ConvertToObj(htmlContent);
        }

        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static TradeDataExtensions ConvertToObj(string Content)
        {

            TradeDataExtensions transData = null;
            if (string.IsNullOrWhiteSpace(Content))
                return transData;
            //var data = 
            //{
            //    name : '兴化股份',
            //    ltg : 210084207,
            //    zgb : 701963193,
            //    mgsy : 0.1425,
            //    mgjzc : 4.5968
            //}
            Regex reg_Json = new Regex(@"var\s*data =\s*\{(.*?)\}", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (!reg_Json.IsMatch(Content))
                return transData;
            transData = new TradeDataExtensions();
            var jsonContent = reg_Json.Match(Content).Groups[1].Value;
            if (!string.IsNullOrWhiteSpace(jsonContent))
            {
                transData.Zgb = Convert.ToDecimal(RequestHelp.striphtml(Regex.Match(jsonContent, "zgb\\s*:\\s*(\\d+)", RegexOptions.IgnoreCase).Groups[1].Value));
                transData.Ltg = Convert.ToDecimal(RequestHelp.striphtml(Regex.Match(jsonContent, "ltg\\s*:\\s*(\\d+)", RegexOptions.IgnoreCase).Groups[1].Value));
                transData.Income = Convert.ToDecimal(RequestHelp.striphtml(Regex.Match(jsonContent, "mgsy\\s*:\\s*(\\d+\\.?\\d+)", RegexOptions.IgnoreCase).Groups[1].Value));
                transData.Mgjzc = Convert.ToDecimal(RequestHelp.striphtml(Regex.Match(jsonContent, "mgjzc\\s*:\\s*(\\d+\\.?\\d+)", RegexOptions.IgnoreCase).Groups[1].Value));
            }
            Regex reg_Table = new Regex(@"var\s*data.*?</table>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            string tableHtml = reg_Table.Match(Content).Value;
            if (!string.IsNullOrWhiteSpace(tableHtml))
            {
                MatchCollection mc = regTR.Matches(tableHtml);
                if (mc != null)
                {
                    var tr0 = regTD.Matches(mc[0].Value);
                    transData.YesterdayCost = Convert.ToDecimal(RequestHelp.striphtml(tr0[5].Value));
                    var tr2 = regTD.Matches(mc[2].Value);
                    transData.Support = RequestHelp.striphtml(tr2[1].Value);
                    transData.Pressure = RequestHelp.striphtml(tr2[3].Value);

                    var tr3 = regTD.Matches(mc[3].Value);
                    var keyMonth = regKeyValue.Match(tr3[3].Value);

                    transData.MonthDiffPrice = Convert.ToDecimal(keyMonth.Groups[1].Value);
                    transData.MonthTradeRate = RequestHelp.striphtml(keyMonth.Groups[2].Value);

                    var keyWeek = regKeyValue.Match(tr3[1].Value);
                    transData.WeekDiffPrice = Convert.ToDecimal(keyWeek.Groups[1].Value);
                    transData.WeekTradeRate = RequestHelp.striphtml(keyWeek.Groups[2].Value);
                    var keyYear = regKeyValue.Match(tr3[5].Value);
                    transData.YearDiffPrice = Convert.ToDecimal(keyYear.Groups[1].Value);
                    transData.YearTradeRate = RequestHelp.striphtml(keyYear.Groups[2].Value);


                }
            }

            return transData;
        }
    }
}
